package com.jkgh.logger {
	import flash.external.ExternalInterface;
	
	public class Log {
		
		private static var active:Boolean;
		
		public static function setActive(a:Boolean):void {
			active = a;
		}
		
		public static function isActive():Boolean {
			return active;
		}
		
		private static function getLocation():Array {
			var line:String = getStackLine();
						
			var cns:int = line.lastIndexOf("\\")+1;
			var cne:int = line.lastIndexOf(":");
			var className:String = line.substring(cns, cne);
			
			var mns:int = line.indexOf("/")+1;
			var mne:int = line.indexOf("()");
			var methodName:String = line.substring(mns, mne);
			
			var lns:int = line.lastIndexOf(":")+1;
			var lne:int = line.lastIndexOf("]");
			var lineNumber:String = line.substring(lns, lne);
			
			return [className, methodName, lineNumber];
		}
		
		private static function getStackLine():String {
			var raw:String = new Error().getStackTrace();
			var lines:Array = raw.split("\n\t", 6);
			return lines[5];
		}
		
		private static function log(level:String, ... parts):void {
			if (active) {
				var location:Array = getLocation();
				var message:String = "["+location[1]+"("+location[0]+":"+location[2]+")"+"] ";
				for each (var p:Object in parts[0]) {
					message += p;
				}
				ExternalInterface.call("console."+level, message);
			}
		}
		
		public static function debug(... message):void {
			log("debug", message);
		}
		
		public static function info(... message):void {
			log("info", message);
		}
		
		public static function warn(... message):void {
			log("warn", message);
		}
		
		public static function error(... message):void {
			log("error", message);
		}
	}
}